package com.shenfa.action.systemManager;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.shenfa.bean.Function;
import com.shenfa.bean.Person;
import com.shenfa.service.MyService;
import com.shenfa.service.systemManager.SystemService;

import com.shenfa.util.ResultObject;


public class SystemAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	/**
	 * 返回的结果集对象
	 */
	public JSONObject result;
	
	private HttpServletRequest request;
	
	private Function func;
	private static Logger log=Logger.getLogger(SystemAction.class);
	
	//分页
	private int start;
	private int limit;
	
	@Autowired
	private MyService myService;
	
	@Autowired
	private SystemService systemService;
	
	/**
	 * 取得用户可操作功能
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String fetchMenuResource(){
		func=new Function();
		func.setParent("0");
		log.debug("id="+func.getId()+",level="+func.getLevel()+",name="+func.getName()+",parent="+func.getParent()+",remark="+func.getRemark()
				+",state="+func.getState()+",url="+func.getUrl());
		List<Function> list = systemService.getList(func);
		List<Function> list2=null;
		if (list == null){
			result = ResultObject.build("-1", null);
		}else{
			
			JSONArray array=new JSONArray();
			JSONArray array2=new JSONArray();
			JSONObject obj1=new JSONObject();
			JSONObject obj2=new JSONObject();
			for(Function fun : list){
				obj1=new JSONObject();
				array2=new JSONArray();
				obj1.put("id", fun.getId());//设置ID
				obj1.put("text", fun.getName());//设置显示名
				func.setParent(fun.getId());//设置父节点
				list2=systemService.getList(func);//查询子节点
				for(Function fun2: list2){
					obj2=new JSONObject();
					obj2.put("id", fun2.getId());//设置ID
					obj2.put("text", fun2.getName());//设置显示名
					obj2.put("url", fun2.getUrl());//设置显示名
					array2.add(obj2);
				}
				obj1.put("children",array2);//设置子节点
				array.add(obj1);
			}
			result = ResultObject.build("0",array);
		}
		
		return SUCCESS;
	}
	
	/**
	 * 获取当前登录用户的权限菜单
	 */
	@SuppressWarnings("unchecked")
	public String qurCurrentPersonMenu(){
		request = ServletActionContext.getRequest();
		Person currentPerson=(Person) request.getSession().getAttribute("login");
		if(null!=currentPerson){
			log.debug("id="+currentPerson.getId());
			List<Function> list = systemService.getMenuListByCurrentPerson(currentPerson.getId());
			if (list == null){
				result = ResultObject.build("-1", null);
			}else{
				JSONArray array=new JSONArray();
				JSONArray array2=new JSONArray();
				JSONObject obj1=new JSONObject();
				JSONObject obj2=new JSONObject();
				JSONArray array3=new JSONArray();//存储对应角色所有的资源URL
				for(Function fun : list){
					if(("0").equals(fun.getParent())){
						obj1=new JSONObject();
						array2=new JSONArray();
						obj1.put("id", fun.getId());//设置ID
						obj1.put("text", fun.getName());//设置显示名
						for(Function fun2 : list){
							//判断子节点fuID是否和当前的fuId相同
							if(!("0").equals(fun2.getParent())&& (obj1.get("id")).equals(fun2.getParent())){
								obj2=new JSONObject();
								obj2.put("id", fun2.getId());//设置ID
								obj2.put("text", fun2.getName());//设置显示名
								obj2.put("url", fun2.getUrl());//设置显示名
								array2.add(obj2);
								array3.add(fun2.getUrl());//获取对应角色所有的资源URL
							}
						}
					
					obj1.put("children", array2);
					array.add(obj1);
					}
				}
				result = ResultObject.build("0",array);
				request.getSession().setAttribute("role_resource", array3);
				
			}
		}
		
		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String fetchPerson(){
		List<Person> list = myService.deomPersonList();
		int count = list.size();
		int begin = start>count?count: start;
		int end = start + limit;
		if (end > count)
			end = count;
		
		list = list.subList(begin, end);
		
		result = ResultObject.build("0", list);
		result.put("total", count);
		return SUCCESS;
	}
	
	public Function getFunc() {
		return func;
	}

	public void setFunc(Function func) {
		this.func = func;
	}

	public void setStart(int start) {
		this.start = Math.abs(start);
	}
	public void setLimit(int limit) {
		this.limit = Math.abs(limit);
	}
}
